package com.njwb.day07.user.service.impl;

import com.njwb.day07.user.dao.UserDao;
import com.njwb.day07.user.dao.impl.UserDaoImpl;
import com.njwb.day07.user.entity.User;
import com.njwb.day07.user.service.UserService;

//用户的业务逻辑接口实现类：定义处理用户的具体业务逻辑表现形式
public class UserServiceImpl implements UserService {

    @Override
    public String regist(User user) {
        //用户名是否在数据中存在
        UserDao userDao=new UserDaoImpl();
        User u=userDao.selectUserByUsername(user.getUsername());
        //存在：用户已存在，不能注册
        if(u!=null){
            return "用户名已存在";
        }
        //不存在：用户不存在，正常注册
        userDao.insertUser(user);
        return "注册成功";
    }

    @Override
    public String login(String username, String password) {
        UserDao userDao=new UserDaoImpl();
        User u=userDao.selectUserByUsernamePassword(username,password);
        if(u==null){
            return "用户名密码不正确";
        }
        return "登录成功";
    }

    @Override
    public User findUserByUsername(String username) {
        UserDao userDao=new UserDaoImpl();
        User u=userDao.selectUserByUsername(username);
        if (u==null){
            return null;
        }
        return u;
    }
}
